package com.controller;

import com.dao.EnvelopeDao;
import com.po.Envelope;
import com.po.FriendList;
import com.po.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet({"/function/envelope", "/function/myEnvelope", "/function/openEnvelope", "/personalInformation", "/changeState"
        , "/selectEnvelope"})
public class FunctionSystem extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        String servletPath = request.getServletPath();
        if (session != null && session.getAttribute("id") != null) {
            if ("/function/envelope".equals(servletPath)) {
                doCreateEnvelope(request, response);
            } else if ("/function/myEnvelope".equals(servletPath)) {
                doSelectMyEnvelope(request, response);
            } else if ("/function/openEnvelope".equals(servletPath)) {
                doSelectOpenEnvelope(request, response);
            } else if ("/personalInformation".equals(servletPath)) {
                doSearch(request, response);
            } else if ("/changeState".equals(servletPath)) {
                doChange(request, response);
            } else if ("/selectEnvelope".equals(servletPath)) {
                doSelectEnvelope(request, response);
            }
        } else {
            response.sendRedirect(request.getContextPath() + "/index.jsp");
        }
    }

    private void doSelectEnvelope(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("UTF-8");
        String envelope_name = request.getParameter("envelope_name");
        List<Envelope> envelopes = EnvelopeDao.selectEnvelopes(envelope_name);

        if (envelopes.isEmpty()) {
            out.println("对不起，没有此信封");
            out.println("<a href=\"" + request.getContextPath() + "/function/openEnvelope\">返回上一级</a>");
        } else {
            request.setAttribute("envelopes", envelopes);
            request.getRequestDispatcher("/envelope/selectEnvelope.jsp").forward(request, response);
        }
    }

    private void doChange(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String state = request.getParameter("state");
        int envelope_id = Integer.parseInt(request.getParameter("envelope_id"));
        EnvelopeDao.changeStatus(envelope_id, state);
        response.sendRedirect(request.getContextPath() + "/function/myEnvelope");
    }

    private void doSearch(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        int id = (int) session.getAttribute("id");
        User user = FriendList.getUser(id);
        request.setAttribute("user", user);
        request.getRequestDispatcher("/me.jsp").forward(request, response);
    }

    private void doSelectOpenEnvelope(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        HttpSession session = request.getSession(false);
        int id = (int) session.getAttribute("id");
        List<Envelope> list = EnvelopeDao.getOpenEnvelope(id);

        List<String> fname = FriendList.getFriendsName(id);
        request.setAttribute("fname", fname);
        request.setAttribute("list", list);
        request.getRequestDispatcher("/envelope/openEnvelope.jsp").forward(request, response);
    }

    private void doSelectMyEnvelope(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        HttpSession session = request.getSession(false);
        PrintWriter out = response.getWriter();
        int id = (int) session.getAttribute("id");

        if(session.getAttribute("note_name")!=null && session.getAttribute("note_content")!=null){
            session.removeAttribute("note_name");
            session.removeAttribute("note_content");
        }

        String sql = "select * from envelope where user_id = ?";
        List<Envelope> envelopes = EnvelopeDao.getEnvelope(id, sql);

        if (!envelopes.isEmpty()) {
            request.setAttribute("envelopes", envelopes);
            request.getRequestDispatcher("/envelope/MyEnvelope.jsp").forward(request, response);
        } else {
            out.print("亲，你还没有信封，请前往");
            out.println("<a href=\"" + request.getContextPath() + "/envelope/createEnvelope.jsp\">创建信封</a>");
        }
    }

    private void doCreateEnvelope(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        String envelope_name = request.getParameter("envelope_name");
        String state = request.getParameter("state");
        HttpSession session = request.getSession();
        int id = (int) session.getAttribute("id");
        if (envelope_name.length() >= 1) {
            int count = EnvelopeDao.createEnvelope(envelope_name, state, id);

            if (count == 1) {
                out.print("<h3>创建成功</h3>");
                out.println("<a href=\"" + request.getContextPath() + "/success.jsp\">返回用户列表</a>");
            } else {
                out.print("<h3>创建失败</h3>");
                out.println("<a href=\"" + request.getContextPath() + "/success.jsp\">返回用户列表</a>");
            }
        }else{
            out.print("对不起，信封名字不能为空");
        }
    }
}
